package com.ydj.framework.exception;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * 普通的异常，内部使用
 */
@Setter
@Getter
@Accessors(chain = true)
public class CommonException extends Exception {

    // 是否需要通知到企业微信
    private boolean reportToWorkWechat = true;

    public CommonException() {
        super();
    }

    public CommonException(String message) {
        super(message);
    }

    public CommonException(String message, Throwable cause) {
        super(message, cause);
    }

    public CommonException(Throwable cause) {
        super(cause);
    }

    protected CommonException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public static CommonException throwException(String errMsg) throws CommonException {
        throw new CommonException(errMsg);
    }

    public static void throwException(String errMsg, boolean reportToWorkWechat) throws CommonException {
        throw new CommonException(errMsg).setReportToWorkWechat(reportToWorkWechat);
    }

    public static void throwException(String errMsg, Throwable cause) throws CommonException {
        throw new CommonException(errMsg, cause);
    }

    public static void throwExceptionIfTrue(boolean condistion, String errMsg) throws CommonException {
        if (condistion) {
            throw new CommonException(errMsg);
        }
    }

    public static void throwExceptionIfTrue(boolean condistion, String errMsg, boolean reportToWorkWechat) throws CommonException {
        if (condistion) {
            throwException(errMsg, reportToWorkWechat);
        }
    }

    public static void throwExceptionIfFalse(boolean condistion, String errMsg) throws CommonException {
        if (!condistion) {
            throw new CommonException(errMsg);
        }
    }

    public static void throwExceptionIfNull(Object obj, String errMsg) throws CommonException {
        if (obj == null) {
            throw new CommonException(errMsg);
        }
        if (obj instanceof String && StringUtils.isEmpty((String) obj)) {
            throw new CommonException(errMsg);
        }
    }

    public static void throwExceptionIfEmpty(Collection collection, String errMsg) throws CommonException {
        if (collection.isEmpty()) {
            throw new CommonException(errMsg);
        }
    }
}